package com.ajk.server.dao.provider;

import com.ajk.server.model.Knowledge;
import java.text.MessageFormat;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;

import com.ajk.server.model.query.KnowledgeQuery;
import com.ajk.server.utils.IntegerUtil;
/**
 * 健康资讯表  <br>
 * t_knowledge <br>
 * v1.0 2016-07-01
 * @see com.ajk.server.dao.KnowledgeMapper
 */
 public class KnowledgeProvider {
 
 	/**
 	* 新增 健康资讯表
 	*/
 	public String insert(Knowledge domain){
		StringBuilder sb = new StringBuilder();
		
		sb.append("insert into t_knowledge(create_time,last_modify_time");
	 	if(domain.getTitle()!=null){
			sb.append(",title");
		}
	 	if(domain.getExcerpt()!=null){
			sb.append(",excerpt");
		}
	 	if(domain.getPicUrl()!=null){
			sb.append(",pic_url");
		}
	 	if(domain.getContent()!=null){
			sb.append(",content");
		}
	 	if(domain.getReadCount()!=null){
			sb.append(",read_count");
		}
	 	if(domain.getPublishTime()!=null){
			sb.append(",publish_time");
		}
		
		sb.append(") values ( now(),now() ");
		
	 	if(domain.getTitle()!=null){
			sb.append(",#{title}");
		}
	 	if(domain.getExcerpt()!=null){
			sb.append(",#{excerpt}");
		}
	 	if(domain.getPicUrl()!=null){
			sb.append(",#{picUrl}");
		}
	 	if(domain.getContent()!=null){
			sb.append(",#{content}");
		}
	 	if(domain.getReadCount()!=null){
			sb.append(",#{readCount}");
		}
	 	if(domain.getPublishTime()!=null){
			sb.append(",#{publishTime}");
		}
		sb.append(")");
		
		return sb.toString();
	}

	/**
 	* 更新 健康资讯表
 	*/
	public String update(Knowledge domain){
		StringBuilder sb = new StringBuilder();
		
		sb.append("update t_knowledge set last_modify_time = now() ");
		
		if(domain.getTitle()!=null){
			sb.append(",title=#{title}");
		}
		if(domain.getExcerpt()!=null){
			sb.append(",excerpt=#{excerpt}");
		}
		if(domain.getPicUrl()!=null){
			sb.append(",pic_url=#{picUrl}");
		}
		if(domain.getContent()!=null){
			sb.append(",content=#{content}");
		}
		if(domain.getReadCount()!=null){
			sb.append(",read_count=#{readCount}");
		}
		if(domain.getPublishTime()!=null){
			sb.append(",publish_time=#{publishTime}");
		}
		
		sb.append(" where id=#{id}");
		
		return sb.toString();
	}
	
	public String batchInsert(Map<String, List<Knowledge>> map){
		StringBuffer sb = new StringBuffer();
		List<Knowledge> knowledges = map.get("list");
		sb.append("insert into t_knowledge(create_time,last_modify_time,title,excerpt,pic_url,content,read_count,publish_time) values");
	    MessageFormat messageFormat = new MessageFormat("("
	    		+ "now(),now()"
	    		+ ",#'{'list[{0}].title}"
	    		+ ",#'{'list[{0}].excerpt}"
	    		+ ",#'{'list[{0}].picUrl}"
	    		+ ",#'{'list[{0}].content}"
	    		+ ",#'{'list[{0}].readCount}"
	    		+ ",#'{'list[{0}].publishTime}"
	    		+")");
	    		
	    if(CollectionUtils.isNotEmpty(knowledges)) {
			sb.append(messageFormat.format(new Integer[]{0}));
			for (int i = 1; i <  knowledges.size(); i++) {
				sb.append(",").append(messageFormat.format(new Integer[]{i}));
			}
		}
		return sb.toString();
	}
	
	/**
 	*通过query 查询 健康资讯表
 	*/
	public String findByQuery(KnowledgeQuery query){
		StringBuffer sb = new StringBuffer();
		sb.append("select id, title, excerpt, pic_url,read_count, publish_time, create_time, last_modify_time from t_knowledge where 1=1 ");
		
		if(IntegerUtil.isPositiveInteger(query.getStartId())){
			sb.append(" and id>#{startId} order by id limit #{limit}");
		}else if(IntegerUtil.isPositiveInteger(query.getEndId())){
			sb.append(" and id<#{endId} order by id desc limit #{limit}");
		}else if(query.getStart()!=null && query.getLimit()!=null && query.getLimit()!=0){
			sb.append(" order by id desc limit #{start},#{limit}");
		}else if(query.getLimit()!=null && query.getLimit()!=0){
			sb.append(" order by id desc limit #{limit}");
		}
		return sb.toString();
	}
	
	public String countByQuery(KnowledgeQuery query){
		StringBuffer sb = new StringBuffer();
		sb.append("select count(1) from t_knowledge ");
		return sb.toString();
	}
	
	/**
 	*通过ids 查询 健康资讯表
 	*/
 	public String findByIds(Map<String, List<Integer>> map){
		StringBuffer sb = new StringBuffer();
		List<Integer> ids = map.get("list");
		sb.append("select * from t_knowledge where ");
		if(ids==null || ids.isEmpty()){
			sb.append(" 1=2");
		}else{
			if(ids.size()==1){
				sb.append(" id=").append("#{list[0]}");
			}else{
				sb.append(" id in (").append("#{list[0]}");
				for(int i=1; i<ids.size() ; i++){
					sb.append(",").append("#{list[").append(i).append("]}");
				}
				sb.append(")");
			}
		}
		return sb.toString();
	}
	
 }	